// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;

namespace CharityGamesOnline.Modules.Account
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// SQL Server implementation of the abstract DataProvider class 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class SqlDataProvider : DataProvider
    {


        #region "Private Members"

        private const string ProviderType = "data";
        private const string ModuleQualifier = "CGO_";

        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;

        #endregion

        #region "Constructors"

        public SqlDataProvider()
        {

            // Read the configuration specific information for this provider 
            Provider objProvider = (Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider];

            // Read the attributes for this provider 

            //Get Connection string from web.config 
            _connectionString = Config.GetConnectionString();

            if (_connectionString == "")
            {
                // Use connection string specified in provider 
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false)
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false)
            {
                _databaseOwner += ".";
            }

        }

        #endregion

        #region "Properties"

        public string ConnectionString
        {
            get { return _connectionString; }
        }

        public string ProviderPath
        {
            get { return _providerPath; }
        }

        public string ObjectQualifier
        {
            get { return _objectQualifier; }
        }

        public string DatabaseOwner
        {
            get { return _databaseOwner; }
        }

        #endregion

        #region "Private Methods"

        private string GetFullyQualifiedName(string name)
        {
            return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;
        }

        private object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        
        #region "Account Methods"
	    public override IDataReader GetAccount(int portalID, int userID)
	    {
		    return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("AccountGet"), portalID, userID);
	    }

	    public override IDataReader GetAccounts(int portalID)
	    {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("AccountGetList"), portalID);
	    }
	    
	    public override void SaveAccount(int portalID, int userID, decimal token)
	    {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("AccountSave"), portalID, userID, GetNull(token));
	    }

        public override void DeleteAccount(int portalID, int userID)
	    {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("AccountDelete"), portalID, userID);
	    }

        public override IDataReader GetAccountLogs(int portalID, int userID, bool getCount, int pageSize, int currentPage)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("AccountLogGetList"), portalID, userID, getCount, pageSize, currentPage);
        }

        public override void SaveAccountLog(int portalID, int userID, decimal token, int paymentID, string comment)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("AccountLogSave"), portalID, userID, GetNull(token), GetNull(paymentID), GetNull(comment));            
        }

	    #endregion

        #region "Payment Methods"
        public override IDataReader GetPayment(int portalID, int paymentID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("PaymentGet"), portalID, paymentID);
        }

        public override IDataReader GetPayments(int portalID, int userID, int enrollmentID, int statusID, bool getCount, int pageSize, int currentPage)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("PaymentGetList"), portalID, userID, enrollmentID, statusID, getCount, pageSize, currentPage);
        }

        public override int SavePayment(int paymentID, int portalID, int userID, decimal amount, int statusID, int enrollmentID)
        {
            object retObj = SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("PaymentSave"), paymentID, portalID, userID, amount, statusID, enrollmentID);
            return Convert.ToInt32(retObj);
        }

        public override void DeletePayment(int portalID, int paymentID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("CGO_PaymentDelete"), portalID, paymentID);
        }
        #endregion

        #region "PaymentLog Methods"
        public override IDataReader GetPaymentLog(int paymentLogID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("PaymentLogGet"), paymentLogID);
        }

        public override IDataReader GetPaymentLogs(int paymentID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("PaymentLogList"), paymentID);
        }

        public override int SavePaymentLog(int paymentLogID, int paymentID, string appID, string productKey, int productQuantity, string transactionID, string transType, string approvalCode, bool isTest, string pageNumber, string receiptNumber, string salesOrderNumber)
        {
            object retObj = SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("PaymentLogSave"), paymentLogID, GetNull(paymentID), GetNull(appID), GetNull(productKey), GetNull(productQuantity), GetNull(transactionID), GetNull(transType), GetNull(approvalCode), GetNull(isTest), GetNull(pageNumber), GetNull(receiptNumber), GetNull(salesOrderNumber));
            return Convert.ToInt32(retObj);
        }

        #endregion
    }
}